package cn.dgiot.fd.service.impl;

import java.sql.Timestamp;
import java.util.Date;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.aliyun.oss.common.utils.StringUtils;

import cn.dgiot.fd.dao.IPropertyReportDAO;
import cn.dgiot.fd.model.PropertyReportModel;
import cn.dgiot.fd.service.IPropertyReportService;

/**
 * [属性上报记录]
 * @author
 *
 */
@Service
public class PropertyReportServiceImpl implements IPropertyReportService{
	
	@Autowired
	private IPropertyReportDAO propertyReportDAO;

	@Override
	public void insertPropertyReport(PropertyReportModel propertyReportModel) {
		/** 查询属性是否存在上报记录 */
		PropertyReportModel result = getPropertyReport(propertyReportModel);
		if(result != null && !StringUtils.isNullOrEmpty(result.getIotId())) {
			propertyReportModel.setCreateTime(new Timestamp(new Date().getTime()));
			updatePropertyReport(propertyReportModel);
		}else {
			propertyReportModel.setId(UUID.randomUUID().toString().replace("-", ""));
			propertyReportModel.setCreateTime(new Timestamp(new Date().getTime()));
			propertyReportDAO.insertPropertyReport(propertyReportModel);
		}
	}

	@Override
	public PropertyReportModel getPropertyReport(PropertyReportModel propertyReportModel) {
		return propertyReportDAO.getPropertyReport(propertyReportModel);
	}

	@Override
	public void updatePropertyReport(PropertyReportModel propertyReportModel) {
		propertyReportDAO.updatePropertyReport(propertyReportModel);
	}
}
